package vn.appsmobi.adapter;

import java.util.ArrayList;

import vn.appsmobi.R;
import vn.appsmobi.object.Category;
import vn.appsmobi.ulti.ImageLoader;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CategoryAdapter_ extends BaseAdapter {

	private Context mContext;
	private ArrayList<Category> mArray;
	private ImageLoader imageLoader;
	private boolean haveAdvertise;

	public CategoryAdapter_(Context context, boolean haveAdvertise) {
		this.mContext = context;
		this.haveAdvertise = haveAdvertise;
		mArray = new ArrayList<Category>();
		imageLoader = new ImageLoader(mContext);
	}

	public ArrayList<Category> getArrayJourneys() {
		return mArray;
	}

	public void setArray(ArrayList<Category> arrayJourneys) {
		mArray = arrayJourneys;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (haveAdvertise) {
			return mArray.size() + 1;
		} else {
			return mArray.size();
		}
	}

	@Override
	public Object getItem(int position) {
		return mArray.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	class ViewHolder {
		ImageView imgIcon;
		TextView txtName;
		TextView txtNum;
		TextView txtDownloadTime;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			LayoutInflater li = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = li.inflate(R.layout.item_category, null);

			holder.imgIcon = (ImageView) convertView
					.findViewById(R.id.img_icon);
			holder.txtName = (TextView) convertView.findViewById(R.id.txt_name);
			holder.txtNum = (TextView) convertView.findViewById(R.id.txt_num);
			holder.txtDownloadTime = (TextView) convertView
					.findViewById(R.id.txt_download_time);

			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		setupItemCategory(holder, position);

		return convertView;
	}

	private void setupItemCategory(ViewHolder holder, int position) {
		if (haveAdvertise == true && position == mArray.size()) {
			holder.txtName.setText(mContext.getString(R.string.text_advertise));
			holder.txtNum.setVisibility(View.INVISIBLE);
			holder.txtDownloadTime.setVisibility(View.INVISIBLE);
		} else {
			Category category = mArray.get(position);
			if (!TextUtils.isEmpty(category.getIconLink())) {
				imageLoader
						.DisplayImage(category.getIconLink(), holder.imgIcon);
			}
			holder.txtName.setText(category.getName());
			holder.txtNum.setVisibility(View.VISIBLE);
			holder.txtDownloadTime.setVisibility(View.VISIBLE);
			holder.txtNum.setText(mContext.getString(R.string.text_num)
					+ category.getNum());
			holder.txtDownloadTime.setText(category.getDownloadTimes()
					+ mContext.getString(R.string.text_download_time));
		}
	}
}
